import apiClient from '@/api/axios.js';

export class TransactionOrderApi {

  // 查询所有交易订单
  static async getAllTransactionOrders() {
    try {
      const response = await apiClient.get('/transactionorder');
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 orderID 查询交易订单
  static async getTransactionOrderByOrderId(orderID) {
    try {
      const response = await apiClient.get(`/transactionorder/${orderID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 buyerID 查询交易订单
  static async getTransactionOrdersByBuyerId(buyerID) {
    try {
      const response = await apiClient.get(`/transactionorder/buyer/${buyerID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 sellerID 查询交易订单
  static async getTransactionOrdersBySellerId(sellerID) {
    try {
      const response = await apiClient.get(`/transactionorder/seller/${sellerID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 itemID 查询交易订单
  static async getTransactionOrdersByItemId(itemID) {
    try {
      const response = await apiClient.get(`/transactionorder/item/${itemID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 新增交易订单
  static async createTransactionOrder(params) {
    const { buyerID, sellerID, itemID, buyerReview, sellerReview, buyerRating, sellerRating, creationTime, status } = params;
    try {
      const response = await apiClient.post('/transactionorder', {
        buyerID,
        sellerID,
        itemID,
        buyerReview,
        sellerReview,
        buyerRating,
        sellerRating,
        creationTime,
        status
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 更新交易订单
  static async updateTransactionOrder(orderID, params) {
    const { buyerID, sellerID, itemID, buyerReview, sellerReview, buyerRating, sellerRating, creationTime, status } = params;
    try {
      const response = await apiClient.put(`/transactionorder/${orderID}`, {
        orderID,
        buyerID,
        sellerID,
        itemID,
        buyerReview,
        sellerReview,
        buyerRating,
        sellerRating,
        creationTime,
        status
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除交易订单
  static async deleteTransactionOrder(orderID) {
    try {
      const response = await apiClient.delete(`/transactionorder/${orderID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
